﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

using DbSharp.Framework.Diff;
using DbSharp.Framework.Storage.IO;
using DbSharp.Framework.Storage;
using DbSharp.Framework.TestEngine;
using DbSharp.GuiRunner.View;
using DbSharp.Utils;
using DbSharp.GuiRunner.Properties;
using DbSharp.Framework.ReleaseManage;
using DbSharp.Framework.DbObject;
 

namespace DbSharp.GuiRunner.UI
{
    partial class FormDiff : Form
    {
        #region Private members

        private BindingList<SetStyleDiffViewItem> m_ListSetStyleDiffViewItem = new BindingList<SetStyleDiffViewItem>();
        private BindingList<TextLineDiffViewItem> m_ListTextLineDiffViewItem = new BindingList<TextLineDiffViewItem>();
        private object m_TargetResultset;
        private bool m_TargetResultsetIsRecordset;
        private List<string> m_ListTargetRecordsetFieldName;
        private object m_ReferResultset;
        private bool m_ReferResultsetIsRecordset;
        private List<string> m_ListReferRecordsetFieldName;
        private bool m_ResultsetAvailable;
        private bool m_CanPerformSetDiff;

        private ITextLineDiffOutputer m_TextLineDiffOutputer;
#if Express
        //do nothing
#else // Professional or Trial
        private XmlRecordsetDiff m_RecordsetDiff;
        private ISetDiffOutputer m_SetDiffOutputer;
#endif
        private string m_CaseNameWithAssertion = string.Empty;

        #endregion

        public FormDiff()
        {
            InitializeComponent(); 
            setStyleDiffFilesGrid.AutoGenerateColumns = false;
            setStyleDiffFilesGrid.DataSource = m_ListSetStyleDiffViewItem;

            textLineDiffFilesGrid.AutoGenerateColumns = false;
            textLineDiffFilesGrid.DataSource = m_ListTextLineDiffViewItem;
        }

        #region Private methods


        private void LoadSetStyleDiffFileFormat()
        {
            toolStripComboBoxDiffFormat.Items.Clear();

            var diffFormats = (SetStyleDiffFileFormat[])Enum.GetValues(typeof(SetStyleDiffFileFormat));
            foreach (var item in diffFormats)
            {
                toolStripComboBoxDiffFormat.Items.Add(item);
            }
        }

        private void LoadSetStyleEmptyDiffItems()
        {
            m_ListSetStyleDiffViewItem.Clear();
            SetStyleDiffMode[] diffMode = (SetStyleDiffMode[])Enum.GetValues(typeof(SetStyleDiffMode));
            foreach (var mode in diffMode)
            {
                var viewItem = new SetStyleDiffViewItem();
                viewItem.DiffMode = mode;
                m_ListSetStyleDiffViewItem.Add(viewItem);
            }
        }

        private void LoadTextLineEmptyDiffItems()
        {
            m_ListTextLineDiffViewItem.Clear();
            TextLineDiffMode[] diffMode = (TextLineDiffMode[])Enum.GetValues(typeof(TextLineDiffMode));
            foreach (var mode in diffMode)
            {
                var viewItem = new TextLineDiffViewItem();
                viewItem.DiffMode = mode;
                m_ListTextLineDiffViewItem.Add(viewItem);
            }
        }

        private void LoadExportedFiles()
        {
            if (!UnitTestRunner.CaseExecuteResult.ExportedFlag)
            {
                buttonExportXml.Enabled = true;
                buttonLaunchDiffTool.Enabled = false;
                return;
            }
            else
            {
                buttonExportXml.Enabled = false;
                buttonLaunchDiffTool.Enabled = true;
                string file1 = UnitTestRunner.CaseExecuteResult.TargetRsltsetExportFile;
                string file2 = UnitTestRunner.CaseExecuteResult.ReferRsltsetExportFile;

                textBoxTargetExportedFile.Text = file1;
                textBoxReferExportedFile.Text = file2;
            }
        }

        private void LoadAssertionResultsets()
        {
            m_ResultsetAvailable = false;
            m_CanPerformSetDiff = false;
            if ((UnitTestRunner.TargetStmtRunner == null) || (UnitTestRunner.ReferStmtRunner == null))
                return;

            object resultset1;
            List<string> listFieldOutputName1;
            List<FieldMetaData> outputFieldMetaDataQueue1;
            bool isRecordset1;
            int indexInList1;
            bool found1;
            found1 = UnitTestRunner.TargetStmtRunner.TryGetAssertionResultset(out resultset1, out listFieldOutputName1, out outputFieldMetaDataQueue1, out isRecordset1, out indexInList1);
            if (found1)
            {
                object resultset2;
                List<string> listFieldOutputName2;
                List<FieldMetaData> outputFieldMetaDataQueue2;
                bool isRecordset2;
                int indexInList2;
                bool found2;
                found2 = UnitTestRunner.ReferStmtRunner.TryGetAssertionResultset(out resultset2, out listFieldOutputName2, out outputFieldMetaDataQueue2, out isRecordset2, out indexInList2);
                if (found2)
                {
                    if (isRecordset1 && isRecordset2)
                    {
                        EnableSetDiffOperateButtons(true);
                        EnableSequenceDiffOperateButtons(true);
                    }
                    else
                    {
                        EnableSetDiffOperateButtons(false);
                        EnableSequenceDiffOperateButtons(true);
                    }

                    m_TargetResultset = resultset1;
                    m_TargetResultsetIsRecordset = isRecordset1;
                    m_ListTargetRecordsetFieldName = listFieldOutputName1;
                    m_ReferResultset = resultset2;
                    m_ReferResultsetIsRecordset = isRecordset2;
                    m_ListReferRecordsetFieldName = listFieldOutputName2;
                    m_ResultsetAvailable = true;
                    m_CanPerformSetDiff = VerifyRecordsetsHaveSameFields(m_ListTargetRecordsetFieldName, m_ListReferRecordsetFieldName);

                    if (m_CanPerformSetDiff == false)
                    {
                        if (isRecordset2 && isRecordset1)
                            label_Hint.Text = "Set style diff is disabled. Fields name and amount in target resultset and reference resultset must be identical.";
                        else
                            label_Hint.Text = "Set style diff is disabled. Both target resultset and reference resultset must be recordset.";
                    }
                    else
                    {
                        label_Hint.Text = "Set style diff is enabled.";
                    }

                }
            }
        }

        private bool VerifyRecordsetsHaveSameFields(List<string> actualFieldList, List<string> expectedFieldList)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();

            foreach (var fieldName in actualFieldList)
            {
                sb1.AppendLine(fieldName);
            }

            foreach (var fieldName in expectedFieldList)
            {
                sb2.AppendLine(fieldName);
            }

            return string.Equals(sb1.ToString(), sb2.ToString(), StringComparison.Ordinal);
        }

        private void EnableSequenceDiffOperateButtons(bool enabled)
        {
            toolStripButtonSequenceDiff.Enabled = enabled;
        }


        private void EnableSetDiffOperateButtons(bool enabled)
        {
            toolStripButtonShowSurplus.Enabled = enabled;
            toolStripButtonShowChanged.Enabled = enabled;
            toolStripButtonShowCommon.Enabled = enabled;
            toolStripButtonShowAbsence.Enabled = enabled;
            toolStripButtonShowUnion.Enabled = enabled;
        }

        private void FormDiff_Load(object sender, EventArgs e)
        {
            if (UnitTestRunner == null)
            {
                this.Enabled = false;
            }
            else
            {
                string fmt = "{0}[{1}]";
                m_CaseNameWithAssertion = String.Format(fmt, UnitTestRunner.CaseName, UnitTestRunner.TestCase.Assertion);
                this.Text = "Diff-" + m_CaseNameWithAssertion;

                EnableSequenceDiffOperateButtons(false);
                EnableSetDiffOperateButtons(false);

                LoadSetStyleEmptyDiffItems();
                LoadTextLineEmptyDiffItems();

                LoadExportedFiles();
                LoadAssertionResultsets();
            }

            if (m_CanPerformSetDiff == false)
            {
                setStyleDiffFilesGrid.Enabled = false;
                toolStrip_SetStyleDiff.Enabled = false;
            }

            if (m_ResultsetAvailable == false)
            {
                panelClient.Enabled = false;
                toolStrip_SetStyleDiff.Enabled = false;
                toolStrip_TextLineDiff.Enabled = false;
            }

            //default diff format is Excel 2007
            LoadSetStyleDiffFileFormat();
            toolStripComboBoxDiffFormat.SelectedIndex = 0;
        }

        private void buttonLaunchDiffTool_Click(object sender, EventArgs e)
        {
            string diffApp = Settings.Default.DiffToolPath;
            if (!File.Exists(diffApp))
            {
                MessageBox.Show("Please specify one external diff tool firstly in Menu tools/options.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string file1 = textBoxTargetExportedFile.Text;
            string file2 = textBoxReferExportedFile.Text;
            string argsFmt = @"""{0}""  ""{1}"" ";
            string args = string.Format(argsFmt, file1, file2);
            ProcessLauncher.OpenFileByApp(diffApp, args, false);
        }


        private SetStyleDiffFileFormat GetCurrentSetStyleDiffFormat()
        {
            SetStyleDiffFileFormat diffFormat;
            try
            {
                diffFormat = (SetStyleDiffFileFormat)(toolStripComboBoxDiffFormat.SelectedItem);
            }
            catch
            {
                diffFormat = SetStyleDiffFileFormat.Excel2007;
            }
            return diffFormat;
        }

        private ISetDiffOutputer GetSetStyleDiffOutputer(SetStyleDiffFileFormat diffFormat)
        {
            ISetDiffOutputer result;
            switch (diffFormat)
            {
                case SetStyleDiffFileFormat.Excel2007:
                    result = new DiffExcelOutputer();
                    break;
                case SetStyleDiffFileFormat.Excel:
                    result = new DiffExcelOutputer();
                    break;
                case SetStyleDiffFileFormat.Html:
                    result = new HtmlSetDiffOutputer();
                    break;
                default:
                    result = null;
                    break;
            }
            return result;
        }

        private ITextLineDiffOutputer GetTextLineDiffOutputer()
        {
            if (m_TextLineDiffOutputer == null)
                m_TextLineDiffOutputer = new HtmlTextLineDiffOutputer();

            return m_TextLineDiffOutputer;
        }

        private SetStyleDiffViewItem LocateSetStyleDiffViewItem(SetStyleDiffMode diffMode)
        {
            SetStyleDiffViewItem result = null;
            foreach (var item in m_ListSetStyleDiffViewItem)
            {
                if (item.DiffMode == diffMode)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }


        private TextLineDiffViewItem LocateTextLineDiffViewItem(TextLineDiffMode diffMode)
        {
            TextLineDiffViewItem result = null;
            foreach (var item in m_ListTextLineDiffViewItem)
            {
                if (item.DiffMode == diffMode)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }


        private string GetDiffFileName(SetStyleDiffFileFormat diffFormat, string diffModeText)
        {
            string diffOutFile;
            switch (diffFormat)
            {
                case SetStyleDiffFileFormat.Excel2007:
                    StorageNameMaker.PrepareExcel2007DiffFileName(diffModeText, out diffOutFile);
                    break;
                case SetStyleDiffFileFormat.Excel:
                    StorageNameMaker.PrepareExcelDiffFileName(diffModeText, out diffOutFile);
                    break;
                case SetStyleDiffFileFormat.Html:
                    StorageNameMaker.PrepareHtmlDiffFileName(diffModeText, out diffOutFile);
                    break;
                default:
                    StorageNameMaker.PrepareExcel2007DiffFileName(diffModeText, out diffOutFile);
                    break;
            }
            return diffOutFile;
        }


        private string GetResultsetText(object resultset, bool isRecordset)
        {
            string text;
            if (isRecordset)
            {
                var writer = new RecordsetXElmtTextWriter((IEnumerable<XElement>)resultset, true, -1);
                text = writer.StringBuilder.ToString();
            }
            else
            {
                text = resultset.ToString();
            }
            return text;
        }


        private void SetBusyStatus()
        {
            this.Cursor =  Cursors.WaitCursor;
            this.Enabled = false;
        }


        private void ResetBusyStatus()
        {
            this.Cursor = Cursors.Default;
            this.Enabled = true;
        }


        private void CreateSetDiffFile(Action specifiedCreateProcedure)
        {
            if (m_CanPerformSetDiff == false)
                return;

            SetBusyStatus();
            try
            {
                if (specifiedCreateProcedure != null)
                    specifiedCreateProcedure();
            }
            finally
            {
                ResetBusyStatus();
            }
        }

        private void CreateTextLineDiffFile(Action specifiedCreateProcedure)
        {
            if (m_ResultsetAvailable == false)
                return;

            SetBusyStatus();
            try
            {
                if (specifiedCreateProcedure != null)
                    specifiedCreateProcedure();
            }
            finally
            {
                ResetBusyStatus();
            }
        }

        private bool CheckAdvDiffEnabled()
        {
            if (RuntimeInfo.GetInstance().AdvancedDiffEnabled == false)
            {
                MessageBox.Show(SensitiveMessage.SetDiffNotSupportedMsg, SensitiveMessage.MsgboxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else
                return true;
        }


        private void CreateChangedDiffFile()
        {
            if (CheckAdvDiffEnabled() == false)
                return;
#if Express
            //do nothing
#else // Professional or Trial

            CreateSetDiffFile(() =>
            {
                #region Initialize set style diff outputter

                SetStyleDiffFileFormat diffFormat = GetCurrentSetStyleDiffFormat();
                m_SetDiffOutputer = GetSetStyleDiffOutputer(diffFormat);
                if (m_SetDiffOutputer == null)
                    return;

                if (m_RecordsetDiff == null)
                    m_RecordsetDiff = new XmlRecordsetDiff((IEnumerable<XElement>)m_TargetResultset, (IEnumerable<XElement>)m_ReferResultset);
                
                #endregion

                var viewItem = LocateSetStyleDiffViewItem(SetStyleDiffMode.Set_Changed);
                if (viewItem == null)
                    return;

                IEnumerable<XElement> listAddedLine;
                IEnumerable<XElement> listDeletedLine;
                m_RecordsetDiff.ShowChanged(out listAddedLine, out listDeletedLine);
                
                var setLegendDisplayFlag = new HashSet<LegendDisplayFlag>();
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayAdded);
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayDeleted);

                string diffModeText = "ChangedDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;
                string diffOutFile;
                diffOutFile = GetDiffFileName(diffFormat, diffModeText);
                try
                {
                    m_SetDiffOutputer.WriteChangedLines(diffOutFile, title, setLegendDisplayFlag, m_ListTargetRecordsetFieldName, listAddedLine, listDeletedLine);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });
#endif
        }


        private void toolStripButtonShowChanged_Click(object sender, EventArgs e)
        {
            CreateChangedDiffFile();
        }


        private void CreateCommonDiffFile()
        {
            if (CheckAdvDiffEnabled() == false)
                return;

#if Express
            //do nothing
#else // Professional or Trial
            CreateSetDiffFile(() =>
            {
                #region Initialize set style diff outputter

                SetStyleDiffFileFormat diffFormat = GetCurrentSetStyleDiffFormat();
                m_SetDiffOutputer = GetSetStyleDiffOutputer(diffFormat);
                if (m_SetDiffOutputer == null)
                    return;

                if (m_RecordsetDiff == null)
                    m_RecordsetDiff = new XmlRecordsetDiff((IEnumerable<XElement>)m_TargetResultset, (IEnumerable<XElement>)m_ReferResultset);

                #endregion

                var viewItem = LocateSetStyleDiffViewItem(SetStyleDiffMode.Set_Common);
                if (viewItem == null)
                    return;

                IEnumerable<XElement> listCommonLine;
                m_RecordsetDiff.ShowCommon(out listCommonLine);

                var setLegendDisplayFlag = new HashSet<LegendDisplayFlag>();
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayCommon);

                string diffModeText = "CommonDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;
                string diffOutFile;
                diffOutFile = GetDiffFileName(diffFormat, diffModeText);
                try
                {
                    m_SetDiffOutputer.WriteCommonLines(diffOutFile, title, setLegendDisplayFlag, m_ListTargetRecordsetFieldName, listCommonLine);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });
#endif
        }


        private void toolStripButtonShowCommon_Click(object sender, EventArgs e)
        {
            CreateCommonDiffFile();
        }



        private void CreateSurplusDiffFile()
        {
            if (CheckAdvDiffEnabled() == false)
                return;

#if Express
            //do nothing
#else // Professional or Trial

            CreateSetDiffFile(() =>
            {
                #region Initialize set style diff outputter

                SetStyleDiffFileFormat diffFormat = GetCurrentSetStyleDiffFormat();
                m_SetDiffOutputer = GetSetStyleDiffOutputer(diffFormat);
                if (m_SetDiffOutputer == null)
                    return;

                if (m_RecordsetDiff == null)
                    m_RecordsetDiff = new XmlRecordsetDiff((IEnumerable<XElement>)m_TargetResultset, (IEnumerable<XElement>)m_ReferResultset);

                #endregion

                var viewItem = LocateSetStyleDiffViewItem(SetStyleDiffMode.Set_Surplus);
                if (viewItem == null)
                    return;

                IEnumerable<XElement> listAddedLine;
                m_RecordsetDiff.ShowAdded(out listAddedLine);

                var setLegendDisplayFlag = new HashSet<LegendDisplayFlag>();
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayAdded);

                string diffModeText = "SurplusDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;
                string diffOutFile;
                diffOutFile = GetDiffFileName(diffFormat, diffModeText);
                try
                {
                    m_SetDiffOutputer.WriteChangedLines(diffOutFile, title, setLegendDisplayFlag, m_ListTargetRecordsetFieldName, listAddedLine, null);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });
#endif
        }


        private void toolStripButtonShowSurplus_Click(object sender, EventArgs e)
        {
            CreateSurplusDiffFile();
        }



        private void CreateAbsenceDiffFile()
        {
            if (CheckAdvDiffEnabled() == false)
                return;

#if Express
            //do nothing
#else // Professional or Trial
            CreateSetDiffFile(() =>
            {
                #region Initialize set style diff outputter

                SetStyleDiffFileFormat diffFormat = GetCurrentSetStyleDiffFormat();
                m_SetDiffOutputer = GetSetStyleDiffOutputer(diffFormat);
                if (m_SetDiffOutputer == null)
                    return;

                if (m_RecordsetDiff == null)
                    m_RecordsetDiff = new XmlRecordsetDiff((IEnumerable<XElement>)m_TargetResultset, (IEnumerable<XElement>)m_ReferResultset);

                #endregion

                var viewItem = LocateSetStyleDiffViewItem(SetStyleDiffMode.Set_Absence);
                if (viewItem == null)
                    return;

                IEnumerable<XElement> listDeletedLine;
                m_RecordsetDiff.ShowDeleted(out listDeletedLine);

                var setLegendDisplayFlag = new HashSet<LegendDisplayFlag>();
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayDeleted);

                string diffModeText = "AbsenceDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;
                string diffOutFile;
                diffOutFile = GetDiffFileName(diffFormat, diffModeText);
                try
                {
                    m_SetDiffOutputer.WriteChangedLines(diffOutFile, title, setLegendDisplayFlag, m_ListTargetRecordsetFieldName, null, listDeletedLine);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });

#endif
        }


        private void toolStripButtonShowAbsence_Click(object sender, EventArgs e)
        {
            CreateAbsenceDiffFile();
        }


        private void CreateUnionDiffFile()
        {
            if (CheckAdvDiffEnabled() == false)
                return;

#if Express
            //do nothing
#else // Professional or Trial

            CreateSetDiffFile(() =>
            {
                #region Initialize set style diff outputter

                SetStyleDiffFileFormat diffFormat = GetCurrentSetStyleDiffFormat();
                m_SetDiffOutputer = GetSetStyleDiffOutputer(diffFormat);
                if (m_SetDiffOutputer == null)
                    return;

                if (m_RecordsetDiff == null)
                    m_RecordsetDiff = new XmlRecordsetDiff((IEnumerable<XElement>)m_TargetResultset, (IEnumerable<XElement>)m_ReferResultset);

                #endregion

                var viewItem = LocateSetStyleDiffViewItem(SetStyleDiffMode.Set_Union);
                if (viewItem == null)
                    return;

                IEnumerable<XElement> listAddedLine;
                IEnumerable<XElement> listDeletedLine;
                IEnumerable<XElement> listCommonLine;
                m_RecordsetDiff.ShowDeleted(out listDeletedLine);
                m_RecordsetDiff.ShowAdded(out listAddedLine);
                m_RecordsetDiff.ShowCommon(out listCommonLine);

                var setLegendDisplayFlag = new HashSet<LegendDisplayFlag>();
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayAdded);
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayDeleted);
                setLegendDisplayFlag.Add(LegendDisplayFlag.DisplayCommon);

                string diffModeText = "UnionDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;
                string diffOutFile;
                diffOutFile = GetDiffFileName(diffFormat, diffModeText);
                try
                {
                    m_SetDiffOutputer.WriteUnionLines(diffOutFile, title, setLegendDisplayFlag, m_ListTargetRecordsetFieldName, listCommonLine, listAddedLine, listDeletedLine);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });

#endif
        }


        private void toolStripButtonShowUnion_Click(object sender, EventArgs e)
        {
            CreateUnionDiffFile();
        }



        private void CreateSequenceDiffFile()
        {
            CreateTextLineDiffFile(() =>
            {
                #region Initialize text line diff outputter

                m_TextLineDiffOutputer = GetTextLineDiffOutputer(); 

                #endregion
                
                var viewItem = LocateTextLineDiffViewItem(TextLineDiffMode.Sequence_Diff);
                if (viewItem == null)
                    return;

                string rev1;
                rev1 = GetResultsetText(m_ReferResultset, m_ReferResultsetIsRecordset);
                string rev2;
                rev2 = GetResultsetText(m_TargetResultset, m_TargetResultsetIsRecordset);
                string diffModeText = "SequenceDiff";
                string title = m_CaseNameWithAssertion + "-" + diffModeText;

                string diffOutFile;
                diffOutFile = GetDiffFileName(SetStyleDiffFileFormat.Html, diffModeText);
                try
                {
                    m_TextLineDiffOutputer.WriteRevisionsDiff(diffOutFile, title, rev1, rev2);
                    viewItem.FileName = diffOutFile;
                }
                catch (Exception ex)
                {
                    viewItem.FileName = string.Empty;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });
        }


        private void toolStripButtonSequenceDiff_Click(object sender, EventArgs e)
        {
            CreateSequenceDiffFile();
        }


        private void setStyleDiffFilesGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SetStyleDiffViewItem selectedRow = (SetStyleDiffViewItem)setStyleDiffFilesGrid.CurrentRow.DataBoundItem;
            if (selectedRow == null)
                return;
            if (e.ColumnIndex == 2)
            {
                switch (selectedRow.DiffMode)
                {
                    case SetStyleDiffMode.Set_Changed:
                        if (toolStripButtonShowChanged.Enabled)
                            CreateChangedDiffFile();
                        break;
                    case SetStyleDiffMode.Set_Common:
                        if (toolStripButtonShowCommon.Enabled)
                            CreateCommonDiffFile();
                        break;
                    case SetStyleDiffMode.Set_Surplus:
                        if (toolStripButtonShowSurplus.Enabled)
                            CreateSurplusDiffFile();
                        break;
                    case SetStyleDiffMode.Set_Absence:
                        if (toolStripButtonShowAbsence.Enabled)
                            CreateAbsenceDiffFile();
                        break;
                    case SetStyleDiffMode.Set_Union:
                        if (toolStripButtonShowUnion.Enabled)
                            CreateUnionDiffFile();
                        break;
                    default:
                        break;
                }
            }
            if (e.ColumnIndex == 3)
            {
                if (File.Exists(selectedRow.FileName))
                    ProcessLauncher.OpenFileByOS(selectedRow.FileName, false);
            }
        }


        private void textLineDiffFilesGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            TextLineDiffViewItem selectedRow = (TextLineDiffViewItem)textLineDiffFilesGrid.CurrentRow.DataBoundItem;
            if (selectedRow == null)
                return;
            if (e.ColumnIndex == 2)
            {
                switch (selectedRow.DiffMode)
                {
                    case TextLineDiffMode.Sequence_Diff:
                        if (toolStripButtonSequenceDiff.Enabled)
                            CreateSequenceDiffFile();
                        break;
                    default:
                        break;
                }
            }
            if (e.ColumnIndex == 3)
            {
                if (File.Exists(selectedRow.FileName))
                    ProcessLauncher.OpenFileByOS(selectedRow.FileName, false);
            }
        }


        private void buttonExportXml_Click(object sender, EventArgs e)
        {
            var runType = CaseRunType.ExportXml;
            UnitTestRunner.Run(runType, TestStatusNotify);
            LoadExportedFiles();
        }

        #endregion

        #region Public property/method


        public UnitTestRunner UnitTestRunner { get; set; }

        public ITestCaseStatusNotify TestStatusNotify { get; set; }

        #endregion

 

    }
}
